//
//  ReadRecordViewController.m
//  B-72K
//
//  Created by sos on 16/3/19.
//  Copyright © 2016年 sos. All rights reserved.
//

#import "ReadRecordViewController.h"
#import "DataBaseManager.h"
#import "BrowesModel.h"
#import "NewsDetialViewController.h"

@interface ReadRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *readRecordTable;
@property (nonatomic, strong) NSMutableArray *readData;
@end

@implementation ReadRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"阅读记录";
    // Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = NO;
    self.readRecordTable = [[UITableView alloc]initWithFrame:self.view.frame];
    self.readRecordTable.delegate = self;
    self.readRecordTable.dataSource = self;
    
    [self.readRecordTable registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(quxiao)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shanchu"] style:UIBarButtonItemStylePlain target:self action:@selector(quxiao)];
    
    [self.view addSubview:self.readRecordTable];
    
    [self dataHandle];
}
-(void)quxiao
{
    
    
    [[DataBaseManager shareInstance] dropBrowseDb];
    
    // 创建浏览数据表
    [[DataBaseManager shareInstance] createBrowseDb];
    
    self.readData = [NSMutableArray array];
    
    //NSLog(@"%@",_dataArr);
    [_readRecordTable reloadData];
    
}

-(void)dataHandle
{
    
    // 数据
    NSMutableArray *arr = [[DataBaseManager shareInstance] selectBrwose];
    NSArray *dataArr = [[arr reverseObjectEnumerator] allObjects];
    self.readData = [NSMutableArray arrayWithArray:dataArr];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.readData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    BrowesModel *m = _readData[indexPath.row];
    cell.textLabel.text = m.title;
    
    
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsDetialViewController *ndVC = [[NewsDetialViewController alloc] init];
    
    BrowesModel *m = _readData[indexPath.row];
    
    ndVC.str = m.feedId;
    
    [self.navigationController pushViewController:ndVC animated:YES];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
 
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
